package com.link2room.soundwave.facade.impl;

import com.greencloud.api.dto.RateEveryDayDto;
import com.greencloud.api.request.RateQueryEveryDayRequest;
import com.greencloud.api.response.RateQueryEveryDayResponse;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.result.base.HotelBaseInfoDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.BatchUtil;
import com.link2room.common.util.DateUtil;
import com.link2room.enums.exception.SoundwaveSysExceptionEnum;
import com.link2room.ironhide.dto.param.inside.HotelBaseInfoInsideParamDto;
import com.link2room.optimus.dto.param.base.PriceStaticBaseParamDto;
import com.link2room.optimus.dto.result.base.PriceBaseDto;
import com.link2room.optimus.dto.result.base.PriceDto;
import com.link2room.optimus.dto.result.base.PriceStaticDayDto;
import com.link2room.optimus.dto.result.base.PriceStaticWeekDto;
import com.link2room.soundwave.constant.SoundwaveMnsConstant;
import com.link2room.soundwave.constant.SoundwaveTaskNameConstant;
import com.link2room.soundwave.dto.param.base.PmsRateParamDto;
import com.link2room.soundwave.entity.L2RIpms2Hotel;
import com.link2room.soundwave.entity.L2RIpms2Rmtype;
import com.link2room.soundwave.exception.SoundwaveException;
import com.link2room.soundwave.feign.ironhide.HotelBaseInfoFeignService;
import com.link2room.soundwave.feign.optimus.PriceBaseFeignService;
import com.link2room.soundwave.feign.optimus.StaticPriceFeignService;
import com.link2room.soundwave.util.GcPmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Created by VegetaKo on 2016/11/15.
 */
@Component("gcPmsRateHandlerFacadeService")
public class GcPmsRateHandlerFacadeServiceImpl extends AbstractPmsRateHandlerFacadeServiceImpl{

    @Resource
    private ICommonService commonService;
    @Autowired
    private HotelBaseInfoFeignService hotelBaseInfoFeignService;
    @Autowired
    private PriceBaseFeignService priceBaseFeignService;
    @Autowired
    private StaticPriceFeignService staticPriceFeignService;


    @Override
    public void syncPMSRate(PmsRateParamDto pmsRateParamDto) throws Exception {
        HotelBaseInfoInsideParamDto hotelBaseInfoInsideParamDto = new HotelBaseInfoInsideParamDto();
        hotelBaseInfoInsideParamDto.setLhotelGroupCode(pmsRateParamDto.getLhotelGroupCode());
        hotelBaseInfoInsideParamDto.setLhotelCode(pmsRateParamDto.getLhotelCode());
        QueryResult<HotelBaseInfoDto> hotelBaseInfoDtoQueryResult= hotelBaseInfoFeignService.hotelBaseInfoList(hotelBaseInfoInsideParamDto.paramToMap()).getQueryResult();
        if(null!=hotelBaseInfoDtoQueryResult&& hotelBaseInfoDtoQueryResult.getResultlist().size()>0 ){
            List<PmsRateParamDto> pmsRateParamDtos=new ArrayList<>();
            for (HotelBaseInfoDto hotelBaseInfoDto : hotelBaseInfoDtoQueryResult.getResultlist()) {
                PmsRateParamDto pmsRateParamDto1 = new PmsRateParamDto();
                pmsRateParamDto1.setLhotelGroupCode(hotelBaseInfoDto.getLhotelGroupCode());
                pmsRateParamDto1.setLhotelCode(hotelBaseInfoDto.getLhotelCode());
                pmsRateParamDto1.setDayStep(pmsRateParamDto.getDayStep());
                pmsRateParamDto1.setStartDate(pmsRateParamDto.getStartDate());
                pmsRateParamDto1.setEndDate(pmsRateParamDto.getEndDate());
                pmsRateParamDtos.add(pmsRateParamDto1);
            }
            BatchUtil.beginBatchTask(SoundwaveMnsConstant.QUEUE_PMSRATE,
                    SoundwaveTaskNameConstant.SAVE_PMS_RATE,
                    pmsRateParamDtos);
        }
    }

    @Override
    public void syncPMSHotelRate(PmsRateParamDto pmsRateParamDto) throws Exception {
        QueryResult<PriceBaseDto> priceBaseDtoQueryResult = priceBaseFeignService.getPriceBases(pmsRateParamDto.paramToMap()).getQueryResult();
        for (PriceBaseDto pbd : priceBaseDtoQueryResult.getResultlist()) {
            PriceStaticBaseParamDto priceStaticParamDto = new PriceStaticBaseParamDto();
            priceStaticParamDto.setLhotelGroupCode(pmsRateParamDto.getLhotelGroupCode());
            priceStaticParamDto.setLhotelCode(pmsRateParamDto.getLhotelCode());
            priceStaticParamDto.setPriceCode(pbd.getPriceCode());
            priceStaticParamDto.setStartDatetime(pmsRateParamDto.getStartDatetime());
            priceStaticParamDto.setEndDatetime(pmsRateParamDto.getEndDatetime());

            QueryResult<PriceDto> priceDtoQueryResult = staticPriceFeignService.getStaticPrices(priceStaticParamDto.paramToMap()).getQueryResult();
            if (null != priceDtoQueryResult & priceDtoQueryResult.getResultlist().size() > 0) {
                L2RIpms2Hotel queryLH=new L2RIpms2Hotel();
                queryLH.setLhotelGroupCode(pmsRateParamDto.getLhotelGroupCode());
                queryLH.setLhotelCode(pmsRateParamDto.getLhotelCode());
                queryLH.setIsHalt(DefaultValue.F);
                L2RIpms2Hotel l2RIpms2Hotel=commonService.findOneEQ(L2RIpms2Hotel.class,queryLH);
                if(l2RIpms2Hotel!=null){
                    List<String> errorRmtypes=new ArrayList<>();
                    for (PriceDto pd : priceDtoQueryResult.getResultlist()) {
                        List<PriceStaticWeekDto> priceStaticWeekDtos = new ArrayList<>();
                        List<PriceStaticDayDto> priceStaticDayDtosNew = new ArrayList<>();
                        //获取PMS对应房型价格
                        L2RIpms2Rmtype  queryLR = new L2RIpms2Rmtype();
                        queryLR.setLhotelGroupCode(l2RIpms2Hotel.getLhotelGroupCode() );
                        queryLR.setLhotelCode(l2RIpms2Hotel.getLhotelCode() );
                        queryLR.setL2RRmtype(pd.getRmtype() );
                        L2RIpms2Rmtype l2RIpms2Rmtype = commonService.findOneEQ(L2RIpms2Rmtype.class, queryLR);
                        if(l2RIpms2Rmtype!=null){
                            RateQueryEveryDayRequest rateQueryEveryDayRequest=RateQueryEveryDayRequest.newInstance();
                            rateQueryEveryDayRequest.setHotelGroupId(l2RIpms2Hotel.getHotelGroupId());
                            rateQueryEveryDayRequest.setHotelId(l2RIpms2Hotel.getHotelId());
                            rateQueryEveryDayRequest.setDate(DateUtil.getWebDateString(new Date()));
                            rateQueryEveryDayRequest.setDayCount(pmsRateParamDto.getDayStep().intValue());
                            rateQueryEveryDayRequest.setRateCode(pbd.getPriceCode());
                            rateQueryEveryDayRequest.setRmType(l2RIpms2Rmtype.getPmsRmtype());
                            RateQueryEveryDayResponse rateQueryEveryDayResponse= GcPmsUtil.getGcClient(pmsRateParamDto.getLhotelGroupCode()).execute(rateQueryEveryDayRequest);
                            List<RateEveryDayDto> rateEveryDayDtos = rateQueryEveryDayResponse.getQueryResult();
                            //如果从PMS获得数据
                            if (null != rateEveryDayDtos && rateEveryDayDtos.size() > 0) {
                                //获取基础价
                                BigDecimal staticPrices = null;
                                BigDecimal weekPrices = null;
                                if (null != pd.getStaticPrices() && pd.getStaticPrices().size() > 0) {
                                    staticPrices = pd.getStaticPrices().get(0).getPrice();
                                }
                                //获取星期价格
                                if (null != pd.getWeekPrices() && pd.getWeekPrices().size() > 0) {
                                    priceStaticWeekDtos.addAll(pd.getWeekPrices());
                                }
                                for (RateEveryDayDto er : rateEveryDayDtos) {

                                    if (null != priceStaticWeekDtos && priceStaticWeekDtos.size() > 0) {
                                        Integer week = DateUtil.getDateWeek(er.getDate());
                                        for (PriceStaticWeekDto pswd : priceStaticWeekDtos) {
                                            if (pswd.getWeekd().equals(week)&&pswd.getIsHalt().equals(DefaultValue.F)  ) {
                                                weekPrices= pswd.getPrice();
                                            }
                                        }
                                    }
                                    boolean flag;
                                    flag= Optional.ofNullable(staticPrices).filter(value->value.compareTo(new BigDecimal(er.getRate1()))!=0).map(value->true).orElse(false);
                                    if(!flag){
                                        flag=Optional.ofNullable(weekPrices).filter(value->value.compareTo(new BigDecimal(er.getRate1()))!=0).map(value->true).orElse(false);
                                    }
                                    if(flag){
                                        PriceStaticDayDto priceStaticDayDto = new PriceStaticDayDto();
                                        priceStaticDayDto.setPrice(new BigDecimal(er.getRate1()));
                                        priceStaticDayDto.setLhotelGroupCode(pd.getLhotelGroupCode());
                                        priceStaticDayDto.setLhotelCode(pd.getLhotelCode());
                                        priceStaticDayDto.setPriceCode(pbd.getPriceCode());
                                        priceStaticDayDto.setRmtype(pd.getRmtype());
                                        priceStaticDayDto.setPriceDate(er.getDate());
                                        priceStaticDayDtosNew.add(priceStaticDayDto);
                                    }

                                }
                            }
                            //priceStaticParamDto.setStaticPrices(priceStaticDtos);
                            // priceStaticParamDto.setWeekPrices(priceStaticWeekDtos);
                            priceStaticParamDto.setRmtype( pd.getRmtype());
                            priceStaticParamDto.setDayPrices(priceStaticDayDtosNew);
                            staticPriceFeignService.updateStaticPrices(priceStaticParamDto);
                        }else {
                            errorRmtypes.add(pd.getRmtype());
                        }
                    }
                    if(errorRmtypes.size()>0){
                        throw new SoundwaveException(SoundwaveSysExceptionEnum.GCPMS_MAPPINGRMTYPE_NOT_EXIT,"缺失映射房型："+errorRmtypes.toArray().toString());
                    }
                }else {
                    throw new SoundwaveException(SoundwaveSysExceptionEnum.GCPMS_MAPPINGHOTEL_NOT_EXIT);
                }
            }
        }
        BatchUtil.doBatchTaskDone(pmsRateParamDto.getTaskMainId(), pmsRateParamDto.getTaskId(), pmsRateParamDto.getIsBatch());
    }

    @Override
    void syncPMSRateIncrement(PmsRateParamDto pmsRateParamDto) throws Exception {

    }

    @Override
    void syncPMSRateHotelIncrement(PmsRateParamDto pmsRateParamDto) throws Exception {

    }
}
